package functions.ems_payment;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.LoggerFactory;

import coms.Constants;
import coms.Constants_zy;
import coms.process.LoginTest;
import frame.HttpHelper;
import functions.ems_claim.InitPaymentPreparation;
/**
 * 付款工作台
 * @author CHENHAILIAN6
 * 2018-08-09
 */
public class Payment {
	private org.slf4j.Logger logger = LoggerFactory.getLogger(Payment.class);
	String payRequestLineId;
	String payRequestLinePayInfoId;
	String result;
    boolean isPassed = false;
	JSONObject resultJsonObject;
	


	public org.slf4j.Logger getLogger() {
		return logger;
	}

	public void setLogger(org.slf4j.Logger logger) {
		this.logger = logger;
	}

	public String getPayRequestLineId() {
		return payRequestLineId;
	}

	public void setPayRequestLineId(String payRequestLineId) {
		this.payRequestLineId = payRequestLineId;
	}

	public String getPayRequestLinePayInfoId() {
		return payRequestLinePayInfoId;
	}

	public void setPayRequestLinePayInfoId(String payRequestLinePayInfoId) {
		this.payRequestLinePayInfoId = payRequestLinePayInfoId;
	}

	public String getResult() {
		return result;
	}

	public void setResult(String result) {
		this.result = result;
	}

	public boolean isPassed() {
		return isPassed;
	}

	public void setPassed(boolean isPassed) {
		this.isPassed = isPassed;
	}

	public JSONObject getResultJsonObject() {
		return resultJsonObject;
	}

	public void setResultJsonObject(JSONObject resultJsonObject) {
		this.resultJsonObject = resultJsonObject;
	}

	/**
	 * 付款准备批页面查询头信息和基本信息
	 * 请求地址：/prepare/info/queryPayPrepareInfo
	 */
	public void queryPayPrepareInfo() {
		String url=Constants_zy.queryPayPrepareInfoUrl;
		String params="";
		try {
			result=HttpHelper.post(params, url);
			JSONObject jsonObject=new JSONObject(result);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	/**
	 * 创建付款申请
	 * @param paramMap
	 */
	public void create(Map<Object, Object> paramMap) {
		if(paramMap == null) {
			try {
				throw new Exception("payRequestIds  必填");
			} catch (Exception e) {
				// TODO Auto-generated catch block
				//e.printStackTrace();
			}
		}
		
		int[] payRequestIds = (int[]) paramMap.get("payRequestIds");
		int size = payRequestIds.length;
		String temp = "";
		for(int i=0;i<size;i++) {
			if (!temp.equals("")) {
				temp += "," + String.valueOf(payRequestIds[i]);
			}else {
			    temp = String.valueOf(payRequestIds[i]);
			}
		}
		String params = "{\"payRequestIds\":["+temp+"]}";
		logger.info(params);
		String url = Constants.PREPARE_CREATE_URL;
		try {
			result=HttpHelper.post(params, url);
			if(HttpHelper.isOk) {
				logger.info("-create--" + result);
				resultJsonObject = new JSONObject(result);
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	public void preExecute() {
		
	}
	
	public void getPayLineDetailsByGroup(Map<Object, Object> paramMap) {
		String batchNo = "";
		if(paramMap !=null) {
			batchNo = (String) paramMap.get("batchNo");
			if(batchNo == null) {
				batchNo = "";
			}
		}
		String params = "{\"batchId\":\"1000000146\"}";
		String url = Constants.GET_PAY_LINE_DETAILS_BY_GROUP_URL;
		try {
			result = HttpHelper.post(params, url);
			if(HttpHelper.isOk) {
				isPassed = HttpHelper.isOk;
				resultJsonObject = new JSONObject(result);
				logger.info("getPayLineDetailsByGroup==============="+result);
			}
			
		} catch (Exception e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
	}
	
	public static void main(String[] args) {
		LoginTest loginTest = new LoginTest();
		InitPaymentPreparation pre = new InitPaymentPreparation();
	
		pre.findInitPaymentPreparationByCondition(null);
		JSONArray array = pre.getInitPaymentPreparations();
		int size = array.length();
		Random dom = new Random(size);
		int temp = dom.nextInt(size -1);
		JSONObject object = array.getJSONObject(temp);
		System.out.println(object.toString());
		int id = (int) object.get("payRequestLineId") ;
		System.out.println(id);
		int[] payRequestIds = new int[1];
		payRequestIds[0] = id;
		HashMap<Object,Object> map = new HashMap<Object,Object>();
		map.put("payRequestIds", payRequestIds);
		Payment payment = new Payment();
		payment.create(map);
		JSONObject resultJsonObject = payment.getResultJsonObject();
		String payBatchId = resultJsonObject.getString("payBatchId");
		String batchNo = resultJsonObject.getString("batchNo");
		PayBatchController payBatch = new PayBatchController();
		map.put("batchNo", batchNo);
		payBatch.queryPayBatchInfo(map);
		map.put("batchId", payBatchId);
		payment.getPayLineDetailsByGroup(map);
		
	}
	
	
}
